package com.onpositive.mediawiki.propertyparser;

import java.util.Collection;
import java.util.HashSet;

import com.onpositive.semantic.model.api.labels.ITextLabelProvider;
import com.onpositive.semantic.model.api.labels.TextProviderAdapter;

public class PropertyGroup {
	
	private String name ;
	private HashSet<String> parents ;
	private ParsedCategory category ;
	
	public PropertyGroup(){}
	
	public PropertyGroup( String name , String parent, ParsedCategory category )
	{
		if ( parent != null && parent.length() != 0 )
		{
			this.parents = new HashSet<String>() ;
			this.parents.add( parent ) ;
		}
		this.name   = name ;
		this.category = category ;
	}
	
	public PropertyGroup( String name , Collection<String> parents, ParsedCategory category )
	{
		if ( parents != null && parents.size() != 0 )
		{
			this.parents = new HashSet<String>() ;
			for( String s : parents )
				this.parents.add( s ) ;
		}
		
		this.name   = name ;
		this.category = category ;
	}
	
	public PropertyGroup( String name , String parent )
	{
		if( parent != null && parent.length() > 0 ){
			this.parents = new HashSet<String>() ;
			this.parents.add( parent ) ;
		}
		
		this.name   = name ;
	}
	
	public PropertyGroup( String name , Collection<String> parents )
	{
		if ( parents != null && parents.size() != 0 )
		{
			this.parents = new HashSet<String>() ;
			for( String s : parents )
				this.parents.add( s ) ;
		}
		
		this.name   = name ;
	}
	
	public void setCategory( ParsedCategory category ){
		this.category = category ;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String[] getParentStrings() {
		if ( parents == null )
			return null ;
		
		String[] result = new String[ parents.size() ] ;
		parents.toArray( result ) ;
		
		return result;
	}
	
	public PropertyGroup[] getParentGroups()
	{
		if( parents == null )
			return null ;
		
		PropertyGroup[] result = new PropertyGroup[ parents.size() ] ;
		int i = 0 ;
		
		for( String s : parents )
			result[i++] = category.getPropertyGroup( s ) ;

		return result;
	}
	
	public void addParent( String parent ) {
		
		if( parent != null && parent.length() != 0 )
		{
			if( parents == null )
				parents = new HashSet<String>() ;
			
			parents.add( parent ) ;
		}
	}
	public void addParent( Collection<String> parents )
	{
		if( parents != null && parents.size() != 0 )
		{
			if( this.parents == null )
				this.parents = new HashSet<String>() ;
			
			for( String s : parents )
				this.parents.add( s ) ;
		}
	}
	
	public static class NameLabelProvider  extends TextProviderAdapter{

		@Override
		public String getText(Object object) {
			return ((PropertyGroup)object).getName();
		}

		@Override
		public String getDescription(Object object) {
			return getText( object );
		}		
	}
	
}
